<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="4">
              <a-form-item label="来源" prop="laiyuan">
                <a-select
                  placeholder="请选择来源"
                  v-model="queryParam.laiyuan"
                  style="width: 100%"
                  allow-clear
                >
                  <a-select-option
                    v-for="(d, index) in laiyuanOptions"
                    :key="index"
                    :value="d.dictValue"
                    >{{ d.dictLabel }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="4">
              <a-form-item label="城市" prop="type">
                <a-select
                  placeholder="所在城市"
                  v-model="queryParam.city"
                  style="width: 100%"
                  allow-clear
                >
                  <a-select-option
                    v-for="(d, index) in cityOptions"
                    :key="index"
                    :value="d.name.replace('市', '')"
                    >{{ d.name }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
            <a-col>
              <span
                class="table-page-search-submitButtons"
                style="float: right"
              >
                <a-button type="primary" @click="handleQuery"
                  ><a-icon type="search" />查询</a-button
                >
                <a-button style="margin-left: 8px" @click="resetQuery"
                  ><a-icon type="redo" />重置</a-button
                >
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 增加 -->
      <biz-xiansuo-chengben-transfer-form
        v-if="showAddModal"
        ref="bizXiansuoChengbenTransferForm"
        :laiyuanOptions="laiyuanOptions"
        :cityOptions="cityOptions"
        :laiyuanType="tabKey"
        @ok="getList"
        @close="showAddModal = false"
      />

      <!-- 编辑 -->
      <biz-xiansuo-chengben-edit-form
        v-if="showEditModal"
        ref="bizXiansuoChengbenEditForm"
        :laiyuanOptions="laiyuanOptions"
        :cityOptions="cityOptions"
        :laiyuanType="tabKey"
        @ok="getList"
        @close="showEditModal = false"
      />
      <a-tabs :activeKey="tabKey" @change="callback">
        <a-tab-pane key="1" tab="广告来源"> </a-tab-pane>
        <a-tab-pane key="2" tab="自建来源"> </a-tab-pane>
      </a-tabs>
      <advance-table
        title="线索成本设置"
        :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) =>
            `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange,
        }"
        tableKey="base-bizXiansuoChengben-index-table"
        rowKey="id"
        size="middle"
        @refresh="getList"
        :columns="columns"
        :data-source="bizXiansuoChengbenList"
        :loading="loading"
        :format-conditions="true"
        @change="handleTableChange"
        :row-selection="{
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
        }"
      >
        <div class="table-operations" slot="button">
          <a-button
            type="primary"
            @click="handleAdd"
            v-hasPermi="['biz:bizXiansuoChengben:add']"
          >
            <a-icon type="plus" />新增
          </a-button>
          <a-button
            type=""
            @click="handleExport"
            v-hasPermi="['biz:bizXiansuoChengben:export']"
          >
            <a-icon type="download" />导出
          </a-button>
          <a-button
            type="danger"
            v-if="!multiple"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['biz:bizXiansuoChengben:remove']"
          >
            <a-icon type="delete" />删除
          </a-button>
        </div>

        <span slot="laiyuan" slot-scope="{ record }">
          {{ laiyuanFormat(record) }}
        </span>

        <span slot="operation" slot-scope="{ text, record }">
          <a
            @click="handleDelete(record)"
            v-hasPermi="['biz:bizXiansuoChengben:remove']"
          >
            删除
          </a>
        </span>
        <span slot="rowindex" slot-scope="{ index }">
          {{ index + 1 }}
        </span>

        <span slot="amount" slot-scope="{ text, record }">
          <a-input-number
            v-model="record.amount"
            :min="0"
            :max="10000"
            placeholder="1-10000"
            :precision="2"
            allow-clear
            style="width: 100%"
            @blur="updateInfo(record)"
          />
        </span>
        <span slot="remark" slot-scope="{ text, record }">
          <a-input v-model="record.remark" @blur="updateInfo(record)" />
        </span>
      </advance-table>
    </a-card>
  </div>
</template>
<script>
import {
  listBizXiansuoChengben,
  delBizXiansuoChengben,
  exportBizXiansuoChengben,
  updateBizXiansuoChengben,
} from "@/api/biz/bizXiansuoChengben";
import AdvanceTable from "@/components/pt/table/AdvanceTable";
import BizXiansuoChengbenEditForm from "@/views/biz/xiansuochengben/modules/BizXiansuoChengbenEditForm";
import BizXiansuoChengbenTransferForm from "@/views/biz/xiansuochengben/modules/BizXiansuoChengbenTransferForm";
import { findListByCode } from "@/api/biz/sysArea";
export default {
  name: "BizXiansuoChengben",
  components: {
    AdvanceTable,
    BizXiansuoChengbenEditForm,
    BizXiansuoChengbenTransferForm,
  },
  data() {
    return {
      showAddModal: false,
      showEditModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 线索成本设置表格数据
      bizXiansuoChengbenList: [],
      // 来源字典
      laiyuanOptions: [],
      // 城市列表
      cityOptions: [],
      // 标签页
      tabKey: "1",
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        laiyuan: undefined,
        city: undefined,
        laiyuanType: "1",
      },
      columns: [
        {
          title: "序号",
          dataIndex: "rowindex",
          ellipsis: true,
          align: "center",
          width: "65px",
          scopedSlots: { customRender: "rowindex" },
        },

        {
          title: "来源",
          dataIndex: "laiyuan",
          // scopedSlots: { customRender: "laiyuan" },
          align: "center",
          width: "180px",
        },
        {
          title: "城市",
          dataIndex: "city",
          align: "center",
          width: "180px",
        },
        {
          title: "成本金额（元/条）",
          dataIndex: "amount",
          ellipsis: true,
          align: "right",
          width: "180px",
          scopedSlots: { customRender: "amount" },
          sorter: true,
        },
        {
          title: "创建时间",
          dataIndex: "createTime",
          align: "center",
          width: "190px",
          sorter: true,
        },
        {
          title: "更新时间",
          dataIndex: "updateTime",
          align: "center",
          width: "190px",
          sorter: true,
        },
        {
          title: "备注",
          dataIndex: "remark",
          ellipsis: true,
          align: "left",
          scopedSlots: { customRender: "remark" },
        },
        {
          title: "操作",
          dataIndex: "operation",
          align: "center",
          width: "150px",
          scopedSlots: { customRender: "operation" },
        },
      ],
    };
  },
  created() {
    this.getList();
    this.getDicts("bus_type").then((response) => {
      this.laiyuanOptions = response.data;
    });
    this.handleFindCityList();
  },
  methods: {
    updateInfo(rows) {
      updateBizXiansuoChengben(rows).then((res) => {
        if (res.code === 200) {
          this.$message.success("操作成功", 3);
          this.getList();
        }
      });
    },

    /** 分页、删选、排序 */
    handleTableChange(pagination, filters, sorter) {
      if (
        sorter.order !== undefined &&
        sorter.order !== null &&
        sorter.order !== ""
      ) {
        this.queryParam.orderByColumn = "a." + sorter.field;
        if (sorter.order === "ascend") {
          this.queryParam.isAsc = "asc";
        } else if (sorter.order === "descend") {
          this.queryParam.isAsc = "desc";
        }
      } else {
        this.queryParam.orderByColumn = null;
        this.queryParam.isAsc = null;
      }
      this.getList();
    },
    /** tab选显卡操作 */
    callback(v) {
      this.tabKey = v;
      this.resetQuery();
      var type = "bus_type";
      if (v === "2") {
        type = "laiyuan_self";
      }
      this.getDicts(type).then((response) => {
        this.laiyuanOptions = response.data;
      });
      this.queryParam.laiyuanType = v;
      this.handleQuery();
    },

    // 获取城市区域列表
    handleFindCityList() {
      findListByCode().then((res) => {
        this.cityOptions = res.data;
      });
    },
    /** 查询线索成本设置列表 */
    getList() {
      this.loading = true;
      listBizXiansuoChengben(this.queryParam).then((response) => {
        this.bizXiansuoChengbenList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 来源字典翻译
    laiyuanFormat(row) {
      if (row.laiyuan) {
        return this.selectDictLabel(this.laiyuanOptions, row.laiyuan);
      } else {
        return "";
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        laiyuan: undefined,
        city: undefined,
      };
      this.handleQuery();
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1;
      this.queryParam.pageSize = size;
      this.getList();
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current;
      this.queryParam.pageSize = pageSize;
      // this.getList();
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
      this.ids = this.selectedRows.map((item) => item.id);
      this.single = selectedRowKeys.length !== 1;
      this.multiple = !selectedRowKeys.length;
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced;
    },
    handleAdd() {
      this.showAddModal = true;
      // this.$nextTick(() => this.$refs.bizXiansuoChengbenAddForm.handleAdd());
      this.$nextTick(() =>
        this.$refs.bizXiansuoChengbenTransferForm.handleAdd()
      );
    },
    handleUpdate(record, ids) {
      this.showEditModal = true;
      this.$nextTick(() =>
        this.$refs.bizXiansuoChengbenEditForm.handleUpdate(record, ids)
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var that = this;
      const bizXiansuoChengbenIds = row.id || this.ids;
      this.$confirm({
        title: "确认删除所选中数据?",
        onOk() {
          return delBizXiansuoChengben(bizXiansuoChengbenIds).then(() => {
            that.onSelectChange([], []);
            that.getList();
            that.$message.success("删除成功", 3);
          });
        },
        onCancel() {},
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      var that = this;
      this.$confirm({
        title: "是否确认导出?",
        content: "此操作将导出当前条件下所有数据而非选中数据",
        onOk() {
          return exportBizXiansuoChengben(that.queryParam).then((response) => {
            that.download(response.msg);
            that.$message.success("导出成功", 3);
          });
        },
        onCancel() {},
      });
    },
  },
};
</script>
